package ca.uwindsor.cs.deepweb.utility;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author Liang Jie
 * 
 */
public class FileToDocumentExtractor {

	protected BufferedReader bufferedreader_in;

	protected long offset;

	protected String path;

	protected String seperator;

	/**
	 * @param path
	 * @param sperator
	 * @param start
	 */
	public FileToDocumentExtractor(String path, String sperator, long start) {
		this.path = path;
		offset = start - 1;
		this.seperator = sperator;
	}

	public void extract(String file) {
		InputStreamReader reader = null;
		String string_line = new String();
		try {
			reader = new InputStreamReader(new FileInputStream(file));
			bufferedreader_in = new BufferedReader(reader);
			string_line = bufferedreader_in.readLine();
			StringBuffer strbuffer = new StringBuffer();
			while (string_line != null) {
				if (string_line.contains(seperator)) {
					String[] documents = string_line.split(seperator);
					if (documents.length == 0) {
						// strbuffer.append(string_line);
						saveTOFile(strbuffer.toString().trim());
						strbuffer = new StringBuffer();
					} else {
						for (int i = 0; i < documents.length; i++) {
							if (!documents[i].equals("")) {
								strbuffer.append(documents[i]);
								saveTOFile(strbuffer.toString().trim());
								strbuffer = new StringBuffer();
							}
						}
					}
				} else {
					if (!string_line.equals("")) {
						strbuffer.append(string_line);
						strbuffer.append(" ");
					}
				}
				string_line = bufferedreader_in.readLine();
			}
			string_line = strbuffer.toString().trim();
			if (!string_line.equals("")) {
				saveTOFile(string_line);
			}
			System.out.println("There are " + offset + " files extracted from "
					+ file);
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	protected void saveTOFile(String buffer) {
		try {
			offset++;
			StringBuffer strbuf_filename = new StringBuffer();
			strbuf_filename.append(path);
			strbuf_filename.append(java.io.File.separator);
			strbuf_filename.append("newsgroup_");
			strbuf_filename.append(offset);
			strbuf_filename.append(".txt");
			FileOutputStream fp = new FileOutputStream(strbuf_filename
					.toString());
			fp.write(buffer.getBytes("UTF-8"));
			fp.flush();
			fp.close();
			System.out.println("Created :" + strbuf_filename.toString());
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
//		 args = new String[4];
//		 args[3] = "D:\\temp";
//		 args[0] = "D:\\mdictwikicontent.txt";
//		 args[1] = "---END.OF.DOCUMENT---";
//		 args[2] = "1";

		if (args.length != 4) {
			System.out
					.println("usage: java FileToDocumentExtractor sourcefile seperator startID resultdirectory");
		} else {
			FileToDocumentExtractor nfe = new FileToDocumentExtractor(args[3],
					args[1], Long.valueOf(args[2]));
			nfe.extract(args[0]);
		}
	}

}
